<?php
namespace app\api\controller;

use Swoole\Client\WebSocket;
use app\api\controller\Kefu;
use think\Controller;
use think\Config;
use think\Cache;
use think\Request;

class Login extends Controller
{
    /**
     * 用户登录
     * @param username 公众号appid
     * @param password 用户openid
     * @param return json
     */
   public function login(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = M('csuser')->field('think_csuser.*,think_author_wx_user.appid,think_author_wx_user.uid,think_author_wx_user.wxname')
                ->where('username',$data['username'])->where('password',md5($data['pwd'] . Config::get('salt')))->join('think_author_wx_user','think_author_wx_user.id = think_csuser.auid')->find();
            // file_put_contents('./requestMsg.log','['.date("Y-m-d H:i:s").'] SqlMsg:'.M('csuser')->getLastSql()."\n",FILE_APPEND);
            if($result){
                $arr_data = [
                    'type' => 'current_csuser_count',
                    'data' => [
                        'id' => $result['auid'],
                        'uid' => $result['uid'],
                    ]
                ];
                sendWebSocket($arr_data);
                return json_encode(['status'=>1,
                    'msg'=>'登录成功',
                    'user_info'=>$result
                ],JSON_UNESCAPED_UNICODE);
            }else{
                return json_encode(['status'=>-1,'msg'=>'登录失败'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            return json_encode(['status'=>-1,'msg'=>'错误'],JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 在线状态
     * @param id 客服id
     * @param return boolean
     */
    public function online($id){
        $result = M('csuser')->where('id',$id)->update(['type' => 1]);
        if($result){
            $wechat_info = M('csuser')->alias('c')->field('a.id,a.uid')->where('c.id',$id)->join('think_author_wx_user a','c.auid = a.id')->find();
            $arr_data = [
                'type' => 'current_csuser_count',
                'data' => [
                    'id' => $wechat_info['id'],
                    'uid' => $wechat_info['uid'],
                ]
            ];
            sendWebSocket($arr_data);
            return json(['status'=>1,'msg'=>'登录成功']);
        }else{
            return json(['status'=>-1,'msg'=>'操作失败']);
        }
    }

    /**
     * 离线状态
     * @param id 客服id
     * @param return boolean
     */
    public function outline($id){
        $result = M('csuser')->where('id',$id)->update(['type' => 3]);
        if($result){
            $wechat_info = M('csuser')->alias('c')->field('a.id,a.uid')->where('c.id',$id)->join('think_author_wx_user a','c.auid = a.id')->find();
            $arr_data = [
                'type' => 'current_csuser_count',
                'data' => [
                    'id' => $wechat_info['id'],
                    'uid' => $wechat_info['uid'],
                ]
            ];
            sendWebSocket($arr_data);
            return json(['status'=>1,'msg'=>'退出成功']);
        }else{
            return json(['status'=>-1,'msg'=>'操作失败']);
        }
    }

    /**
     * 忙碌状态
     * @param id 客服id
     * @param return boolean
     */
    public function busy($id){
        $result = M('csuser')->where('id',$id)->update(['type' => 2]);
        if($result){
            return json(['status'=>1,'msg'=>'忙碌中']);
        }else{
            return json(['status'=>-1,'msg'=>'操作失败']);
        }
    }

    /**
     * 客服手机号码登录
     */
    public function MobileLogin(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = M('kefu')
                        ->field('id,f_ids,username,nickname')
                        ->where('username',$data['username'])
                        ->where('password',md5($data['pwd'] . Config::get('salt')))
                        ->find();
            if($result){
                $kefu = new Kefu;
                $csuserList = $kefu->SvCtmList($result['f_ids']);
                if($csuserList !== false) $result['csuserList'] = $csuserList;
                $time = time();
                $key = md5(time() . $result['username'] . Config::get('salt'));
                M('kefu')->update([
                        'last_login_time' => $time,
                        'last_login_ip'   => $this->request->ip(),
                        'id'              => $result['id'],
                        'key'             => $key,
                    ]);
                // Cache::set($result['id'],$key);
                return json_encode(['status'=>1,'msg'=>'登录成功','user_info'=>$result , 'key' => $key],JSON_UNESCAPED_UNICODE);
            }else{
                return json_encode(['status'=>-1,'msg'=>'登录失败'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            return json_encode(['status'=>-1,'msg'=>'错误'],JSON_UNESCAPED_UNICODE);
        }
    }

    // 新老客户端判断
    public function version(){
        // 获取head里面的 version
        $head_data = Request::instance()->header();
        $isnew = isset($head_data['version']) ? $head_data['version'] : 0;
        $infomation = '';
        if($isnew){
            // 消息来自新客户端,需要转发给老客户端
            $infomation = '消息来自新客户端,需要转发给老客户端';
        }else{
            // 消息来自老客户端,需要转发给新客户端
            $infomation = '消息来自老客户端,需要转发给新客户端';
        }
        return json_encode(['status'=>1,
            'msg'=>'登录成功',
            'isnew' => $head_data['version'],
            'info' => $infomation,
            'head' => Request::instance()->header(),
            'get' => Request::instance()->get(),
            'post' => Request::instance()->post()
        ],JSON_UNESCAPED_UNICODE);
    }
}